﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.Engine
{
    [DomainObject(typeof(IOrderItem))]
    public partial class OrderItem : IOrderItem
    {
        private IDomainReference<IOrderPart> _orderPartLocker;
        private IDomainReference<IPriceItem> _priceItemLocker;

        public OrderItem()
        {
            _orderPartLocker = new SmartReference<OrderItem, OrderPart, IOrderPart>(this, OrderPartReference, it => it.OrderPartID);
            _priceItemLocker = new SmartReference<OrderItem, PriceItem, IPriceItem>(this, PriceItemReference, it => it.PriceItemID);
        }

        IDomainReference<IOrderPart> IOrderItem.OrderPartReference
        {
            get { return _orderPartLocker; }
        }

        IOrderPart IOrderItem.OrderPart
        {
            get
            {
                return _orderPartLocker.Value;
            }
            set
            {
                _orderPartLocker.Value = value;
            }
        }

        IDomainReference<IPriceItem> IOrderItem.PriceItemReference
        {
            get { return _priceItemLocker; }
        }

        IPriceItem IOrderItem.PriceItem
        {
            get
            {
                return _priceItemLocker.Value;
            }
            set
            {
                _priceItemLocker.Value = value;
            }
        }

        public override bool CheckSelfProperties()
        {
            return PriceItemReference.Value != null && OrderPartReference.Value != null;
        }

        decimal IOrderItem.GetCost()
        {
            return Amount != null ? (decimal) Amount.Value * PriceItem.Cost : PriceItem.Cost;
        }
    }
}
